<template>
  <!-- 避免事件冒泡的模态窗口的布局，将蒙层和登录盒子做成兄弟关系 -->
  <div class="login-modal">
    <!--  遮罩层独立一个空盒子 -->
    <div class="mask" @click="changeIsShowLoginModel(false)"></div>
    <div class="login-box">
      <!-- 关闭符合 -->
      <div class="close" @click="changeIsShowLoginModel(false)">
        <img src="../../assets/img/close.png" alt="" />
      </div>

      <ul class="titles">
        <li @click="isShowFrom = true" :class="{ active: isShowFrom }">
          手机号登录
        </li>
        <li style="margin: 0 10px">|</li>
        <li @click="isShowFrom = false" :class="{ active: !isShowFrom }">
          微信扫码登录
        </li>
      </ul>

      <div class="body">
        <div class="form" v-show="isShowFrom">
          <div class="ipt-group">
            <input
              type="text"
              placeholder="请输入手机号"
              maxlength="11"
              v-model.trim="phoneNum"
            />
          </div>
          <div class="ipt-group mb20">
            <slide-verify
              :l="42"
              :r="20"
              :w="290"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
            ></slide-verify>
          </div>
          <div class="ipt-group mb20">
            <input type="text" placeholder="请输入短信" maxlength="6" />
            <div class="btn">
              <span v-show="!isShowCount" @click="getCode">获取验证码</span>
              <span v-show="isShowCount">{{count}}s</span> 
            </div>
          </div>
          <div class="btn" @click="submit">登录</div>
        </div>
        <div class="wx-qrcode" v-show="!isShowFrom">扫码登录内容</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import { validateTelephoneNumber } from "@/utils";
export default {
  data() {
    return {
      // 是否展示登录表单
      isShowFrom: true,
      // 滑块提示语
      msg: "向右滑动",
      // 输入手机号
      phoneNum: "",
      // 最大的计时时间
      countMax: 5,
      // 倒计时时间,每秒变化的那个数字
      count: 60,
      // 是否展示秒数所在盒子
      isShowCount: false,
    };
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModel: "isShowLogin/changeIsShowLoginModel",
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    // 获取验证码
    getCode() {
      //1、验证手机号是否正确
      if (!validateTelephoneNumber(this.phoneNum)) {
        alert("请输入正确手机号");
        return
      }

      // 2、进行滑块验证
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        alert("请滑动拼图");
        return;
      }

      // 3、验证成功后，发起请求，获取验证码成功，则进行倒计时，并展示秒数
      this.isShowCount = true
      this.cuntDown()
    },
    // 点击登录
    submit() {
      // hua
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        alert("请滑动拼图");
        return;
      }
    },
    // 倒计时
    cuntDown() {
      this.count = this.countMax
      let timer  = setInterval(() => {
        this.count--
        if(this.count==0){
          this.isShowCount = false
          clearInterval(timer)
        }
      },1000);
    },
  },
};
</script>

<style scoped lang="less">
@import url("../../assets/css/public.less");
.login-modal {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;

  .mask {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
  }

  .login-box {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 555px;
    height: 423px;
    background: url("../../assets/img/login-box-bg.png");

    .close {
      position: absolute;
      right: 60px;
      top: 20px;
      cursor: pointer;
    }

    .titles {
      margin-top: 60px;
      display: flex;
      justify-content: center;
      color: #ccc;

      li {
        cursor: pointer;
      }
      .active {
        color: #666;
      }
    }

    .body {
      margin-top: 20px;
      display: flex;
      justify-content: center;

      /* 短信表单样式 */
      .form {
        .ipt-group {
          display: flex;

          input {
            height: 48px;
            flex: 1;
            text-indent: 10px;
            border: 1px solid #ccc;
          }

          .btn {
            width: 100px;
            margin-left: 10px;
          }
        }
      }

      .btn {
        height: 50px;
        background-color: @base-color;
        text-align: center;
        line-height: 50px;
        color: #fff;
        cursor: pointer;
      }
    }
  }
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>
